﻿using SmartHRMS.CustomExceptions.Infrastructure;
using SmartHRMS.Infrastructure;
using SmartHRMS.Model.Company;
using StructureMap;
using System;
using System.Text;

namespace SmartHRMS.Presentation.Company
{
    public class CompanyPresenter
    {
        private ICompanyView view;
        private ICompanyService companyProfileService;
        private SmartHRMS.Model.Company.Company companyProfile;
        private Mode mode;

        public CompanyPresenter(ICompanyView view)
        : this(ObjectFactory.GetInstance<ICompanyService>(), view)
        {
        }

        public CompanyPresenter(ICompanyService companyProfileService, ICompanyView view)
        {
            this.view = view;
            this.companyProfileService = companyProfileService;
        }

        public void Display()
        {
            SmartHRMS.Model.Company.Company Company = GetCompanyProfile();

            ClearInfoAndErrorMessages();
            setButtonText();
            setTitleText();
            UpdateView(Company);
        }

        private void setButtonText()
        {
            if (mode == Mode.Add)
            {
                view.SaveButtonText = "Save";
            }
            else
            {
                view.SaveButtonText = "Update";
            }
        }

        private void setTitleText()
        {
            if (mode == Mode.Add)
            {
                view.ViewTitle("Add");
            }
            else
            {
                view.ViewTitle("Edit");
            }
        }

        private void ClearInfoAndErrorMessages()
        {
            view.ErrorMessage(string.Empty);
            view.InfoMessage(string.Empty);
            view.WarringMessage(string.Empty);
        }

        private void DisplayErrorsIn(SmartHRMS.Model.Company.Company company)
        {
            StringBuilder sb = new StringBuilder();

            sb.Append("There were some problems when trying to save this compnay information:");
            sb.Append(Environment.NewLine);
            sb.Append(Environment.NewLine);

            foreach (BrokenRule brokenRule in company.GetBrokenRules())
            {
                sb.Append(brokenRule.Rule);
                sb.Append(Environment.NewLine);
                view.ErrorMessage(sb.ToString());
            }
        }

        private SmartHRMS.Model.Company.Company GetCompanyProfile()
        {
            try
            {
                companyProfile = this.companyProfileService.LoadCompanyProfile();
                if (companyProfile.CompanyId == Guid.Empty)
                {
                    //Add mode
                    companyProfile = new SmartHRMS.Model.Company.Company();
                    mode = Mode.Add;
                }
                else
                {
                    //  Edit mode               
                    mode = Mode.Edit;
                }
            }
            catch (NullReferenceException ex)
            {
                //Add mode
                companyProfile = new SmartHRMS.Model.Company.Company();
                mode = Mode.Add;
            }

            return companyProfile;
        }


        private void UpdateView(SmartHRMS.Model.Company.Company companyProfile)
        {
            view.CompanyName = companyProfile.CompanyName;
            view.EstablishDate = companyProfile.EstablishDate;
            view.Mission = companyProfile.Mission;
            view.Vision = companyProfile.Vision;
            view.Slogan = companyProfile.Slogan;
            view.Logo = companyProfile.Logo;
            if (companyProfile.CompanyAddress != null)
            {
                //load the company address information from database
                CompanyAddress companyAddress = new CompanyAddress(
                    companyProfile.CompanyAddress.State,
                    companyProfile.CompanyAddress.HouseNumber,
                    companyProfile.CompanyAddress.City,
                    companyProfile.CompanyAddress.Country,
                    companyProfile.CompanyAddress.State,
                    companyProfile.CompanyAddress.PostCode,
                    companyProfile.CompanyAddress.OfficePhone,
                    companyProfile.CompanyAddress.Fax,
                    companyProfile.CompanyAddress.Email,
                    companyProfile.CompanyAddress.Website);

                view.OfficePhone = companyAddress.OfficePhone;
                view.OfficePhone = companyAddress.OfficePhone;
                view.Fax = companyAddress.Fax;
                view.Country = companyAddress.Country;
                view.City = companyAddress.City;
                view.State = companyAddress.State;
                view.Street = companyAddress.Street;
                view.HouseNumber = companyAddress.HouseNumber;
                view.PostCode = companyAddress.PostCode;
                view.Email = companyAddress.Email;
                view.Website = companyAddress.Website;
            }
        }

        private void UpdateModel(SmartHRMS.Model.Company.Company companyProfile)
        {
            companyProfile.CompanyName = view.CompanyName;
            companyProfile.EstablishDate = view.EstablishDate;
            companyProfile.Mission = view.Mission;
            companyProfile.Vision = view.Vision;
            companyProfile.Slogan = view.Slogan;
            companyProfile.Logo = view.Logo;

            //load the company address information from the user input
            CompanyAddress companyAddress = new CompanyAddress(
                view.State,
                view.HouseNumber,
                view.City,
                view.Country,
                view.State,
                view.PostCode,
                view.OfficePhone,
                view.Fax, 
                view.Email, 
                view.Website);

            companyProfile.CompanyAddress = companyAddress;
        }

        public void SaveCompanyInformation()
        {
            SmartHRMS.Model.Company.Company company = GetCompanyProfile();
            ClearInfoAndErrorMessages();
            try
            {
                UpdateModel(company);
                if (company.GetBrokenRules().Count == 0)
                {
                    try
                    {
                        this.companyProfileService.Save(company);

                        if (mode == Mode.Add)
                        {
                            this.view.InfoMessage(String.Format("Company information '{0}' has been added.", company.CompanyName));
                        }
                        else
                        {
                            this.view.InfoMessage(String.Format("Company information has been updated successfully."));
                        }
                    }
                    catch (Exception ex)
                    {
                        string exceptionMessage = String.Format("A problem occurred while saving company information '{0}'. {1}{2}", company.CompanyName, Environment.NewLine, ex.Message);
                        view.ErrorMessage(exceptionMessage);
                    }
                }
                else
                {
                    DisplayErrorsIn(company);
                }
            }
            catch (ValueObjectValidationException ex)
            {
                view.ErrorMessage(ex.Message);
            }
        }
    }
}
